home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir24 / jnos110g.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  5KB  |  238 lines

  1. /* Miscellaneous machine independent utilities
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "socket.h"
  6. #include "mbuf.h"
  7.   
  8. /* convert a tcp port number or name to integer - WG7J */
  9. int
  10. atoip(char *s) {
  11.     int p,n;
  12.   
  13.     if((p=atoi(s)) == 0) {
  14.         n = strlen(s);
  15.         if(!strncmp(s,"convers",n))
  16.             p = IPPORT_CONVERS;
  17.         else if(!strncmp(s,"telnet",n))
  18.             p = IPPORT_TELNET;
  19.         else if(!strncmp(s,"ttylink",n))
  20.             p = IPPORT_TTYLINK;
  21.     }
  22.     return p;
  23. }
  24.   
  25. /* Select from an array of strings, or return ascii number if out of range */
  26. char *
  27. smsg(msgs,nmsgs,n)
  28. char *msgs[];
  29. unsigned nmsgs,n;
  30. {
  31.     static char buf[16];
  32.   
  33.     if(n < nmsgs && msgs[n] != NULLCHAR)
  34.         return msgs[n];
  35.     sprintf(buf,"%u",n);
  36.     return buf;
  37. }
  38.   
  39. /* Convert hex-ascii to integer */
  40. int
  41. htoi(s)
  42. char *s;
  43. {
  44.     int i = 0;
  45.     char c;
  46.   
  47.     while((c = *s++) != '\0'){
  48.         if(c == 'x')
  49.             continue;   /* allow 0x notation */
  50.         if('0' <= c && c <= '9')
  51.             i = (i * 16) + (c - '0');
  52.         else if('a' <= c && c <= 'f')
  53.             i = (i * 16) + (c - 'a' + 10);
  54.         else if('A' <= c && c <= 'F')
  55.             i = (i * 16) + (c - 'A' + 10);
  56.         else
  57.             break;
  58.     }
  59.     return i;
  60. }
  61.   
  62. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  63.  * library, but it doesn't call mallocw() and can therefore return NULL.
  64.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  65.  */
  66. char *
  67. strdup(s)
  68. const char *s;
  69. {
  70.     register char *out;
  71.     register int len;
  72.   
  73.     if(s == NULLCHAR)
  74.         return NULLCHAR;
  75.     len = strlen(s);
  76.     out = mallocw(len+1);
  77.     /* This is probably a tad faster than strcpy, since we know the len */
  78.     memcpy(out,s,len);
  79.     out[len] = '\0';
  80.     return out;
  81. }
  82. /* Routines not needed for Turbo 2.0, but available for older libraries */
  83. #ifdef  AZTEC
  84.   
  85. /* Case-insensitive string comparison */
  86. strnicmp(a,b,n)
  87. register char *a,*b;
  88. register int n;
  89. {
  90.     char a1,b1;
  91.   
  92.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  93.         if(a1 == b1)
  94.             continue;   /* No need to convert */
  95.         a1 = tolower(a1);
  96.         b1 = tolower(b1);
  97.         if(a1 == b1)
  98.             continue;   /* NOW they match! */
  99.         if(a1 > b1)
  100.             return 1;
  101.         if(a1 < b1)
  102.             return -1;
  103.     }
  104.     return 0;
  105. }
  106.   
  107. char *
  108. strtok(s1,s2)
  109. char *s1;   /* Source string (first call) or NULL */
  110. #ifdef  __STDC__    /* Ugly kludge for aztec's declaration */
  111. const char *s2; /* Delimiter string */
  112. #else
  113. char *s2;   /* Delimiter string */
  114. #endif
  115. {
  116.     static int isdelim();
  117.     static char *next;
  118.     register char *cp;
  119.     char *tmp;
  120.   
  121.     if(s2 == NULLCHAR)
  122.         return NULLCHAR;    /* Must give delimiter string */
  123.   
  124.     if(s1 != NULLCHAR)
  125.         next = s1;      /* First call */
  126.   
  127.     if(next == NULLCHAR)
  128.         return NULLCHAR;    /* No more */
  129.   
  130.     /* Find beginning of this token */
  131.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  132.         ;
  133.   
  134.     if(*cp == '\0')
  135.         return NULLCHAR;    /* Trailing delimiters, no token */
  136.   
  137.     /* Save the beginning of this token, and find its end */
  138.     tmp = cp;
  139.     next = NULLCHAR;    /* In case we don't find another delim */
  140.     for(;*cp != '\0';cp++){
  141.         if(isdelim(*cp,s2)){
  142.             *cp = '\0';
  143.             next = cp + 1;  /* Next call will begin here */
  144.             break;
  145.         }
  146.     }
  147.     return tmp;
  148. }
  149. static int
  150. isdelim(c,delim)
  151. char c;
  152. register char *delim;
  153. {
  154.     char d;
  155.   
  156.     while((d = *delim++) != '\0'){
  157.         if(c == d)
  158.             return 1;
  159.     }
  160.     return 0;
  161. }
  162. #endif  /* AZTEC */
  163.   
  164.   
  165.   
  166. /* Host-network conversion routines, replaced on the x86 with
  167.  * assembler code in pcgen.asm
  168.  */
  169. #ifndef MSDOS
  170. /* Put a long in host order into a char array in network order */
  171. char *
  172. put32(cp,x)
  173. register char *cp;
  174. int32 x;
  175. {
  176.     *cp++ = x >> 24;
  177.     *cp++ = x >> 16;
  178.     *cp++ = x >> 8;
  179.     *cp++ = x;
  180.     return cp;
  181. }
  182. /* Put a short in host order into a char array in network order */
  183. char *
  184. put16(cp,x)
  185. register char *cp;
  186. int16 x;
  187. {
  188.     *cp++ = x >> 8;
  189.     *cp++ = x;
  190.   
  191.     return cp;
  192. }
  193. int16
  194. get16(cp)
  195. register char *cp;
  196. {
  197.     register int16 x;
  198.   
  199.     x = uchar(*cp++);
  200.     x <<= 8;
  201.     x |= uchar(*cp);
  202.     return x;
  203. }
  204. /* Machine-independent, alignment insensitive network-to-host long conversion */
  205. int32
  206. get32(cp)
  207. register char *cp;
  208. {
  209.     int32 rval;
  210.   
  211.     rval = uchar(*cp++);
  212.     rval <<= 8;
  213.     rval |= uchar(*cp++);
  214.     rval <<= 8;
  215.     rval |= uchar(*cp++);
  216.     rval <<= 8;
  217.     rval |= uchar(*cp);
  218.   
  219.     return rval;
  220. }
  221. /* Compute int(log2(x)) */
  222. int
  223. log2(x)
  224. register int16 x;
  225. {
  226.     register int n = 16;
  227.     for(;n != 0;n--){
  228.         if(x & 0x8000)
  229.             break;
  230.         x <<= 1;
  231.     }
  232.     n--;
  233.     return n;
  234. }
  235.   
  236. #endif
  237.   
  238.